<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")."GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-type: application/json");

require_once '../../../lib/php/vars.php';
require_once 'class.upload.php';

 
if (!empty($_FILES['Filedata'])
		&& isset($_POST['folder'])
		&& isset($_POST['filename'])
		&& isset($_POST['width'])
		&& isset($_POST['height'])
		&& isset($_POST['quality'])){
	$targetPath = $documentRoot.$sitePath.$imageRoot.$_POST['folder']; 

	$handle = new Upload($_FILES['Filedata']);
	if ($handle->uploaded) {
		// save uploaded image with a new name
		$handle->file_new_name_body 	= $_POST['filename'];
		$handle->file_overwrite			= true;
		$handle->file_auto_rename		= false;
		$handle->image_resize			= true;
		$handle->image_x				= 1000;
		$handle->image_y				= 600;
		$handle->image_ratio_no_zoom_in = true;
		$handle->jpeg_quality			= $_POST['quality'];
		$handle->Process($targetPath);
		if ($handle->processed) {
			$json = array('result' 			=> 1,
							'dir'			=> $handle->file_dst_path,
							'filename'		=> $handle->file_dst_name,
							'filepath'		=> $imageRoot.$_POST['folder'].$handle->file_dst_name,
							'imagewidth' 	=> $handle->image_dst_x,
							'imageheight'	=> $handle->image_dst_y,
							'scale_x'		=> $_POST['width'] * 80 - 5,
							'scale_y'		=> $_POST['height'] * 80 - 5,
							'quality'		=> $_POST['quality'],
							'response'		=> 'image "'.$_FILES['Filedata']['name'].'" uploaded and renamed to "'.$handle->file_dst_name.'".',
							'callback'		=> 'savePreview'
							);
			} else {
				$json = array('result' 		=> 0,
								'response'	=> 'image processing failed!\n'
												.$handle->error
				);
			}
	} else {
		$json = array('result' => 0,
						'response'	=> 'upload failed!\n'.$handle->error
		);
	}
	$handle->clean();
} else {
	$json = array('result' => 0, 'response'	=> 'parameters missing\n'
												.$handle->error
		);
}
$encoded = json_encode($json);
echo $encoded;
unset($encoded);
?>